var util = require("../../../utils/util.js"),
{api} = require("../../../utils/api.js"),
    app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabType: 1,
        categroyIndex: 0,
        categroyList: [],
        categroySecondIndex: 0,
        categroySecondList: [],
        goodsList: [],
        userInfo: null,
        integral: null,
        shareLogo: null,
        fristLoading:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        wx.hideShareMenu();
        this.integralIndex();
    },
    async integralIndex(){
        const {data} = await api.IntegralIndex();
        this.setData({shareLogo:data.share_logo})
    },
    tabMethod(e) {
        let type = e.currentTarget.dataset.type;
        this.setData({
            tabType: type
        });
        this.shoppingCategroy();
    },
    gotoPage(e) {
        let {
            url,
            type,
            login
        } = e.currentTarget.dataset;
        if (login == 1 && util.getUserInfo(this.data.userInfo, true) == false)return false;
        if(!url)return false;
        type == 'tab' ? wx.switchTab({
            url
        }) : wx.navigateTo({
            url
        });
    },
    gotoGoodsDetail(e) {
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: `/pages/shopping/goodsDetail/goodsDetail?id=${id}&type=${this.data.tabType}`
        })
    },
    async shoppingCategroy() {
        let {
            data
        } = await api.shoppingCategroy({
            type: this.data.tabType
        });
        this.setData({
            categroyList: data && Array.isArray(data.categroy) ? data.categroy : []
        })
        this.getSecondCategroy();
    },
    getchildlist(e) {
        let index = e.currentTarget.dataset.index;
        this.setData({
            categroyIndex: index
        });
        this.getSecondCategroy();
    },
    getSecondCategroy() {
        const {
            categroyList,
            categroyIndex
        } = this.data;
        let categroySecondList=categroyList.length > categroyIndex?(Array.isArray(categroyList[categroyIndex].childlist) ? categroyList[categroyIndex].childlist : []):[];
        this.setData({categroySecondList,goodsList:[],categroySecondIndex:0});
        if(categroySecondList.length>0){
            this.shoppingList();
        }
    },
    getGoodslist(e) {
        let index = e.currentTarget.dataset.index;
        this.setData({
            categroySecondIndex: index
        });
        this.shoppingList();
    },
    async shoppingList() {
        const {
            categroySecondList,
            categroySecondIndex
        } = this.data;
        let {
            data
        } = await api.shoppingList({
            category_id: categroySecondList[categroySecondIndex].id,
            pageSize: 1000,
            type:(this.data.tabType-1)
        });
        this.setData({
            goodsList: data && data.list && Array.isArray(data.list.data) ? data.list.data : []
        })
    },
    bindpatient(){
        wx.navigateTo({
          url: '/pages/mine/thePatient/thePatient',
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let tabType = wx.getStorageSync('goodsSwitch');
        this.setData({tabType:tabType?tabType:1});
        setTimeout(() => this.shoppingCategroy())
        this.getUsers();
        if(tabType)wx.removeStorageSync('goodsSwitch');
    },
    async getUsers() {
        const data = await app.getUsers();
        if(data){
            if(data.userInfo.is_submit){
                wx.showShareMenu();
            }
            this.setData(data)
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage(e) {
        return {
            title: '摩尔小程序',
            path: '/pages/index/index?share_id=' + this.data.userInfo.id,
            imageUrl:this.data.shareLogo
        }
    }
})